package com.googlecode.jmicroerp.wicket.components;

//import java.util.Arrays;
//import java.util.List;
import java.util.Random;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;

import com.googlecode.jmicroerp.service.ProduseService;


public class NameValidator implements IValidator<String> {

    final private ProduseService produseService;
	
	private static final long serialVersionUID = 1L;
	
	public NameValidator(ProduseService produseService) {
		super();
		this.produseService = produseService;
	}
//	List<String> existingUsernames = Arrays.asList("bigJack", "anonymous", "mrSmith", "C220-RDS");
	public void validate(IValidatable<String> validatable) {
		String chosenUserName = validatable.getValue();
//		String chosenUserName = "C220-RDS";
//		String existingUsernames = produseService.listDenumiri(chosenUserName);
		Object[] existingUsernames = produseService.listDenumiri(chosenUserName);
		
//		if(existingUsernames.contains(chosenUserName)){
		if(existingUsernames != null){
			ValidationError error = new ValidationError(this);
			Random random = new Random();

			error.setVariable("suggestedUserName", validatable.getValue() + random.nextInt());
			validatable.error(error);
		}
	}	
}
